<?php

	include('html_lib.php');

	$link = mysql_connect('127.0.0.1','root','abc123') or die('Could not connect to server');
	mysql_select_db('test',$link) or die('Database test not found');

	mysql_query('create table if not exists dsmh(id char (4) not null, name char (60), primary key(id))') or die ('Could not create table dsmh');

	if(isset($cmd)){
		switch($cmd){
			case 'Ok':
				if (isset($rb) && ($rb !='')) 
					mysql_query ("update dsmh set id='$ma', name='$ten' where id='$rb'");
				else
					mysql_query ("insert into dsmh (id, name) values ('$ma', '$ten')");
				$rb = $id = $name = '';
				mysql_query("delete from dsmh where id=''");
			break;

			case 'Delete':
				if(isset($c))
					foreach($c as $key => $value){
						if($value == '1')
							mysql_query("delete from dsmh where id='{$key}'");
							}
				break;

			case 'Find':
				$sql = mysql_query("select * from dsmh where id like '%$ma%' and name like '%$ten%'");
				
				break;

		}
		
	}
	if(isset($rb)){
		$kq = mysql_query("select * from dsmh where id='$rb'");
		$row = mysql_fetch_array($kq);
		$ma = $row[0];
		$ten = $row[1];
	}

	echo htOpen('List of Course');
	echo formOpen();
	echo tableOpen('','0','','1');
	echo tr( td('Course Name::') . td( textbox('ten',$ten,'30','64') ) );
	echo tr( td('Course Id:') . td( textbox('ma',$ma,'15','4') ) );
	echo tr( td( cmd('Ok') . cmd('Delete') . cmd('Find'),'' ,'' ,'', '2' ) );
	echo tableClose();

	echo tableOpen();

	echo tr(td('STT','10%') .
			td('Del','10%') . 
			td('Edit','10%') . 
			td('Course Id','25%') . 
			td('Course Name','45%'));
	$ci=1;
	if(isset($sql)){
		while($row = mysql_fetch_array($sql)){
			echo tr(td($ci++) .
					td(checkbox("c[{$row[0]}]",'1','') . 
					td(radio($row[0],'rb', $rb, '1'))) . 
					td($row[0]) .
					td($row[1]));
		}
	}
	else {
		$kq = mysql_query("select * from dsmh");
		while($row = mysql_fetch_array($kq)){
			echo tr(td($ci++) .
					td(checkbox("c[{$row[0]}]",'1','') .  
					td(radio($row[0], $name='rb', $rb, '1'))) . 
					td($row[0]) .
					td($row[1]));
		}
	}
	echo tableClose();
	echo formClose();
	echo htClose();

?>